'''
Created on 29 Nov 2012

@author: quirino.brizi
'''

import session
from .messages import Request
from .enums import Method

def request(req, ses):    
    # If session is provided use the provided one retaining the connections on 
    # the pool, otherwise create a new default session and close it at the end.
    provided = True;
    s = ses
    if ses is None :
        s = session.session()
        provided = False
    
    try :
        return s.exchange(req)
    finally:
        if not provided : s.close()

def get(url, qs={}, hdr={}, opt={}, sec=None, auth=None, session=None):
    """
    Executes an HTTP GET request
    @param url: the base URL including port if necessary
    @param qs: the query string parameters  as a dictionary. Default to 
    empty dictionary
    @param hdr: the request headers as a dictionary. Default to empty 
    dictionary.
    @param opt: the dynamic configuration option as a dictionary. Default 
    to empty dictionary
    @param sec: the security implementation as defined on security module. 
    Default to None
    @param auth: the authorization implementation as defined on the 
    authorization module. Default to None.
    @param session: a custom session object, useful for a user specific 
    configuration. If provided, the session is not close in order to get 
    it reusable. If not provided a new session is created and closed as 
    soon that the transaction ends.
    """
    req = Request(url=url, method=Method.GET, qs=qs, hdr=hdr, 
                  opt=opt, sec=sec, auth=auth)
    return request(req, session)

def head(url, qs={}, hdr={}, opt={}, sec=None, auth=None, session=None):
    """
    Executes an HTTP HEAD request
    @param url: the base URL including port if necessary
    @param qs: the query string parameters  as a dictionary. Default to 
    empty dictionary
    @param hdr: the request headers as a dictionary. Default to empty 
    dictionary.
    @param opt: the dynamic configuration option as a dictionary. Default 
    to empty dictionary
    @param sec: the security implementation as defined on security module. 
    Default to None
    @param auth: the authorization implementation as defined on the 
    authorization module. Default to None.
    @param session: a custom session object, useful for a user specific 
    configuration. If provided, the session is not close in order to get 
    it reusable. If not provided a new session is created and closed as 
    soon that the transaction ends.
    """
    req = Request(url=url, method=Method.HEAD, qs=qs, hdr=hdr, 
                  opt=opt, sec=sec, auth=auth)
    return request(req, session)

def post(url, qs={}, hdr={}, opt={}, sec=None, auth=None, files={}, 
         data=None, session=None):
    """
    Executes an HTTP POST request
    @param url: the base URL including port if necessary
    @param qs: the query string parameters  as a dictionary. Default to 
    empty dictionary
    @param hdr: the request headers as a dictionary. Default to empty 
    dictionary.
    @param opt: the dynamic configuration option as a dictionary. Default 
    to empty dictionary
    @param sec: the security implementation as defined on security module. 
    Default to None
    @param auth: the authorization implementation as defined on the 
    authorization module. Default to None.
    @param files: the file to have to send to the remote server as a 
    dictionary. Default to empty dictionary.
    @param data: the row data to have to send to the remote server as a 
    string. Default to None.
    @param session: a custom session object, useful for a user specific 
    configuration. If provided, the session is not close in order to get 
    it reusable. If not provided a new session is created and closed as 
    soon that the transaction ends.
    """
    req = Request(url=url, method=Method.POST, qs=qs, hdr=hdr, 
                  opt=opt, sec=sec, auth=auth, files=files, data=data)
    return request(req, session)

def put(url, qs={}, hdr={}, opt={}, sec=None, auth=None, files={}, 
         data=None, session=None):
    """
    Executes an HTTP PUT request
    @param url: the base URL including port if necessary
    @param qs: the query string parameters  as a dictionary. Default to 
    empty dictionary
    @param hdr: the request headers as a dictionary. Default to empty 
    dictionary.
    @param opt: the dynamic configuration option as a dictionary. Default 
    to empty dictionary
    @param sec: the security implementation as defined on security module. 
    Default to None
    @param auth: the authorization implementation as defined on the 
    authorization module. Default to None.
    @param files: the file to have to send to the remote server as a 
    dictionary. Default to empty dictionary.
    @param data: the row data to have to send to the remote server as a 
    string. Default to None.
    @param session: a custom session object, useful for a user specific 
    configuration. If provided, the session is not close in order to get 
    it reusable. If not provided a new session is created and closed as 
    soon that the transaction ends.
    """
    req = Request(url=url, method=Method.PUT, qs=qs, hdr=hdr, 
                  opt=opt, sec=sec, auth=auth, files=files, data=data)
    return request(req, session)

def delete(url, qs={}, hdr={}, opt={}, sec=None, auth=None, session=None):
    """
    Executes an HTTP DELETE request
    @param url: the base URL including port if necessary
    @param qs: the query string parameters  as a dictionary. Default to 
    empty dictionary
    @param hdr: the request headers as a dictionary. Default to empty 
    dictionary.
    @param opt: the dynamic configuration option as a dictionary. Default 
    to empty dictionary
    @param sec: the security implementation as defined on security module. 
    Default to None
    @param auth: the authorization implementation as defined on the 
    authorization module. Default to None.
    @param session: a custom session object, useful for a user specific 
    configuration. If provided, the session is not close in order to get 
    it reusable. If not provided a new session is created and closed as 
    soon that the transaction ends.
    """
    req = Request(url=url, method=Method.DELETE, qs=qs, hdr=hdr, 
                  opt=opt, sec=sec, auth=auth)
    return request(req, session)

def option(url, qs={}, hdr={}, opt={}, sec=None, auth=None, session=None):
    """
    Executes an HTTP OPTION request
    @param url: the base URL including port if necessary
    @param qs: the query string parameters  as a dictionary. Default to 
    empty dictionary
    @param hdr: the request headers as a dictionary. Default to empty 
    dictionary.
    @param opt: the dynamic configuration option as a dictionary. Default 
    to empty dictionary
    @param sec: the security implementation as defined on security module. 
    Default to None
    @param auth: the authorization implementation as defined on the 
    authorization module. Default to None.
    @param session: a custom session object, useful for a user specific 
    configuration. If provided, the session is not close in order to get 
    it reusable. If not provided a new session is created and closed as 
    soon that the transaction ends.
    """
    req = Request(url=url, method=Method.OPTION, qs=qs, hdr=hdr, 
                  opt=opt, sec=sec, auth=auth)
    return request(req, session)